﻿using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyWpfControls.Pages.OxyPlot
{
    /// <summary>
    /// Line2.xaml 的交互逻辑
    /// </summary>
    public partial class Line2 : Page, IDisposable
    {
        private Timer timer;

        PlotModel model = new PlotModel();
        LineSeries lineSeries = new LineSeries();
        int index = 0;
        int index_value = 0;
        bool index_flag = true;
        DateTime? datetime;

        public Line2()
        {
            InitializeComponent();
            this.timer = new Timer(OnTimerElapsed);

            this.Model0 = CreatePlotModel();

            this.DataContext = this;

            this.timer.Change(300, 100);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
        }

        public PlotModel Model0 { get; private set; }

        private PlotModel CreatePlotModel()
        {

            model.PlotAreaBorderColor = OxyColor.FromArgb(100, 30, 30, 30);

            model.Axes.Add(new LinearAxis()
            {
                MajorGridlineColor = OxyColor.FromArgb(40, 0, 0, 139),
                MajorGridlineStyle = LineStyle.Solid,
                MinorGridlineColor = OxyColor.FromArgb(20, 0, 0, 139),
                MinorGridlineStyle = LineStyle.Dot,
                TicklineColor = OxyColor.FromArgb(100, 30, 30, 30),
                MaximumPadding = 0.5,
                MinimumPadding = 0.5,
                IsZoomEnabled = false,
                IsPanEnabled = false,
            });

            model.Axes.Add(new LinearAxis()
            {
                MajorGridlineColor = OxyColor.FromArgb(40, 0, 0, 139),
                MajorGridlineStyle = LineStyle.Solid,
                MinorGridlineColor = OxyColor.FromArgb(20, 0, 0, 139),
                MinorGridlineStyle = LineStyle.Dot,
                Position = AxisPosition.Bottom,
                TicklineColor = OxyColor.FromArgb(100, 30, 30, 30),
                IsZoomEnabled = false,
                IsPanEnabled = false,
                LabelFormatter = timeStamp =>
                {
                    return timeStamp + "秒";
                }
            });

            model.Series.Add(lineSeries);
            return model;
        }

        private void InsertPoints()
        {
            if (index_value == 30)
            {
                index_flag = false;
            }
            if (index_value == 0)
            {
                index_flag = true;
            }
            if (lineSeries.Points.Count > 300)
            {
                lineSeries.Points.RemoveAt(0);
            }
            lineSeries.Points.Add(new DataPoint((DateTime.Now - datetime.Value).TotalSeconds, index_value));
            index_value += index_flag ? 1 : -1;
            index++;
        }

        private void OnTimerElapsed(object state)
        {
            lock (this.model.SyncRoot)
            {
                this.Update();
            }

            this.model.InvalidatePlot(true);
        }

        private void Update()
        {
            if (datetime == null)
            {
                datetime = DateTime.Now;
            }
            InsertPoints();
        }

        public void Dispose()
        {
            this.timer?.Dispose();
            this.timer = null;
        }
    }
}
